package GUI;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;

import java.util.*;

import logic.Controller;

import objects.Result;

import org.eclipse.swt.events.KeyEvent;
//import java.awt.event.KeyEvent;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DragDetectEvent;
import org.eclipse.swt.events.DragDetectListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.events.KeyAdapter;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.IntellitypeListener;
import com.melloware.jintellitype.JIntellitype;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;

public class userInterface extends Composite implements HotkeyListener,
		IntellitypeListener {

	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());

	private StyledText commandLineInterface = null;

	// variable for global hotkeys
	private static final int WINDOWS_A = 88;
	private static final int ALT_SHIFT_B = 89;
	private static final int CTRL_SHIFT_A = 90;
	private static final int CTRL_SHIFT_S = 91;
	private static final int F9 = 92;
	private Text text_1;

	private boolean returnMouseUp = true;
	private Point locationOfBeforeClicking;

	/*
	 * public void setMouseUpOrDown(boolean x) { returnMouseUp = x; }
	 * 
	 * public boolean returnMouseUpOrDown() { return returnMouseUp; }
	 */

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public userInterface(final Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				toolkit.dispose();
			}
		});
		toolkit.adapt(this);
		toolkit.paintBordersFor(this);
		setLayout(new FormLayout());

		final Composite composite = new Composite(this, SWT.NONE);

		createDragCapabilities(composite);

		FormData fd_composite = new FormData();
		fd_composite.right = new FormAttachment(100, -50);
		fd_composite.left = new FormAttachment(0, 5);
		composite.setLayoutData(fd_composite);
		toolkit.adapt(composite);
		toolkit.paintBordersFor(composite);

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			boolean dialogOpen = false;
			boolean Open = false;

			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!dialogOpen) {
					getShell().setSize(745, 455);
					dialogOpen = true;
				} else {
					getShell().setSize(745, 100);
					dialogOpen = false;
				}
			}
		});
		btnNewButton.setBounds(687, 39, 19, 25);
		toolkit.adapt(btnNewButton, true, true);
		btnNewButton.setText("V");

		Composite bottomLayout = new Composite(this, SWT.NONE);
		fd_composite.bottom = new FormAttachment(bottomLayout);

		createDragCapabilities(bottomLayout);

		final Label commandLabel = new Label(composite, SWT.NONE);
		commandLabel.setBounds(10, 44, 346, 15);
		toolkit.adapt(commandLabel, true, true);
		commandLabel.setText("Available commands: Add, SubAdd, Edit...");
		bottomLayout.setLayout(new FormLayout());
		FormData fd_bottomLayout = new FormData();
		fd_bottomLayout.bottom = new FormAttachment(100, -54);
		fd_bottomLayout.top = new FormAttachment(0, 100);
		fd_bottomLayout.right = new FormAttachment(100, -50);
		fd_bottomLayout.left = new FormAttachment(0, 5);
		bottomLayout.setLayoutData(fd_bottomLayout);
		toolkit.adapt(bottomLayout);
		toolkit.paintBordersFor(bottomLayout);

		// final StyledText
		commandLineInterface = new StyledText(composite, SWT.BORDER);
		commandLineInterface.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {

				if (e.keyCode == SWT.CR) {

					if (commandLineInterface.getText().trim()
							.equals("personalise")) {
						Shell shell = new Shell(getDisplay());
						personaliseWindow calc = new personaliseWindow(shell,
								SWT.NONE);
						calc.pack();
						shell.pack();
						shell.open();
						shell.setText("Personalise Hotkeys");
					}
				
					Result r = Controller.processText(commandLineInterface
							.getText().trim());
					System.out.println(r.isSuccessful());
					System.out.println(r.getError());
					System.out.println(r.getData().get(0));
				}

				String userInput = commandLineInterface.getText() + e.character;

				System.out.println("user input is " + userInput
						+ " and length is " + userInput.length());
				System.out.println("command line interface input is "
						+ commandLineInterface.getText());
				if (userInput.contains("subadd")) {

					commandLabel
							.setText("subadd <task_name> -t <new_task> -d <dd/mm/yy> ");
					int counter = 0;
					StringTokenizer userInputTokenized = new StringTokenizer(
							userInput);
					String tokenizedInput;

					while (userInputTokenized.hasMoreTokens()) {

						tokenizedInput = userInputTokenized.nextToken();

						if (tokenizedInput.equals("-t")) {
							StyleRange xxx = new StyleRange();
							xxx.start = counter;
							xxx.length = 2;
							xxx.foreground = new Color(getDisplay(), 255, 127,
									0);
							commandLineInterface.setStyleRange(xxx);
							// xxx.start = userInput.
						}
						System.out.println("counter is " + counter);
						counter = tokenizedInput.length() + 1 + counter;
						System.out.println("Tokenized input is "
								+ tokenizedInput);
					}
				} else if (userInput.contains("delete")) {
					commandLabel.setText("delete <task_name>");
				} else if (userInput.equals("exit")) {
					System.exit(0);
				} else if ((userInput.length() == 2 && e.keyCode == SWT.BS)
						|| (userInput.length() == 1 && e.keyCode == SWT.BS)) {
					commandLabel
							.setText("Available commands: Add, SubAdd, Edit");
				} else {
					commandLabel.setText("add <task_name> -d <dd/mm/yy>");
				}
			}
		});
		commandLineInterface.setBounds(10, 13, 558, 19);
		toolkit.adapt(commandLineInterface);
		toolkit.paintBordersFor(commandLineInterface);

		Button btnCalendar = new Button(composite, SWT.NONE);
		btnCalendar.addSelectionListener(new SelectionAdapter() {

			// boolean calendarDialogisOpen = false;

			public void widgetSelected(SelectionEvent e) {
				if (!calendarInterface.getDialogOpenStatus()) {
					calendarInterface.createCalendar(getDisplay(), parent,
							commandLineInterface);

				}
			}
		});
		btnCalendar.setBounds(599, 8, 92, 25);
		toolkit.adapt(btnCalendar, true, true);
		btnCalendar.setText("Set Due Date");

		Composite composite_1 = new Composite(bottomLayout, SWT.NONE);
		FormData fd_composite_1 = new FormData();
		fd_composite_1.left = new FormAttachment(0, 10);
		fd_composite_1.right = new FormAttachment(100, -591);
		fd_composite_1.top = new FormAttachment(0, 10);
		fd_composite_1.bottom = new FormAttachment(100, 12);
		composite_1.setLayoutData(fd_composite_1);
		toolkit.adapt(composite_1);
		toolkit.paintBordersFor(composite_1);
		composite_1.setLayout(new FormLayout());

		createDragCapabilities(composite_1);

		ScrolledComposite scrolledComposite = new ScrolledComposite(
				composite_1, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		FormData fd_scrolledComposite = new FormData();
		fd_scrolledComposite.bottom = new FormAttachment(100, -31);
		fd_scrolledComposite.top = new FormAttachment(0, 10);
		fd_scrolledComposite.left = new FormAttachment(0, 10);
		fd_scrolledComposite.right = new FormAttachment(0, 104);
		scrolledComposite.setLayoutData(fd_scrolledComposite);
		scrolledComposite.setExpandVertical(true);
		scrolledComposite.setExpandHorizontal(true);
		toolkit.adapt(scrolledComposite);
		toolkit.paintBordersFor(scrolledComposite);

		Composite composite_2 = new Composite(bottomLayout, SWT.NONE);

		createDragCapabilities(composite_2);
		FormData fd_composite_2 = new FormData();
		fd_composite_2.left = new FormAttachment(composite_1, 6);
		fd_composite_2.right = new FormAttachment(100, -25);
		fd_composite_2.bottom = new FormAttachment(0, 333);
		fd_composite_2.top = new FormAttachment(0, 2);
		composite_2.setLayoutData(fd_composite_2);
		toolkit.adapt(composite_2);
		toolkit.paintBordersFor(composite_2);
		composite_2.setLayout(new FormLayout());

		text_1 = new Text(composite_2, SWT.BORDER);
		text_1.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				System.out.println("I'm searching for " + text_1.getText()
						+ e.character);
			}
		});
		text_1.setMessage("Search tasks here");
		FormData fd_text_1 = new FormData();
		fd_text_1.left = new FormAttachment(0, 10);
		fd_text_1.right = new FormAttachment(0, 371);
		text_1.setLayoutData(fd_text_1);
		toolkit.adapt(text_1, true, true);

		Button button_3 = new Button(composite_2, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		button_3.setText("Open");
		FormData fd_button_3 = new FormData();
		fd_button_3.top = new FormAttachment(0, 8);
		fd_button_3.right = new FormAttachment(0, 478);
		fd_button_3.left = new FormAttachment(0, 392);
		button_3.setLayoutData(fd_button_3);
		toolkit.adapt(button_3, true, true);

		Button button_4 = new Button(composite_2, SWT.NONE);
		button_4.setText("Completed");
		FormData fd_button_4 = new FormData();
		fd_button_4.top = new FormAttachment(0, 8);
		fd_button_4.right = new FormAttachment(0, 555);
		fd_button_4.left = new FormAttachment(0, 469);
		button_4.setLayoutData(fd_button_4);
		toolkit.adapt(button_4, true, true);

		ScrolledComposite scrolledComposite_1 = new ScrolledComposite(
				composite_2, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		fd_text_1.bottom = new FormAttachment(scrolledComposite_1, -6);
		scrolledComposite_1.setExpandVertical(true);
		scrolledComposite_1.setExpandHorizontal(true);
		FormData fd_scrolledComposite_1 = new FormData();
		fd_scrolledComposite_1.bottom = new FormAttachment(0, 287);
		fd_scrolledComposite_1.top = new FormAttachment(0, 37);
		fd_scrolledComposite_1.right = new FormAttachment(0, 555);
		fd_scrolledComposite_1.left = new FormAttachment(0, 10);
		scrolledComposite_1.setLayoutData(fd_scrolledComposite_1);
		toolkit.adapt(scrolledComposite_1);
		toolkit.paintBordersFor(scrolledComposite_1);

		Composite composite_3 = new Composite(scrolledComposite_1, SWT.NONE);
		toolkit.adapt(composite_3);
		toolkit.paintBordersFor(composite_3);

		// dummy events
		/*
		 * ArrayList<Event> listOfEvents = new ArrayList<Event>(); String
		 * taskDescriptionSample = "hehe"; String tag = "hehe2"; Event a = new
		 * Event(taskDescriptionSample, tag, -1, null, null); for (int i = 0; i
		 * < 10; i++) { listOfEvents.add(new Event(taskDescriptionSample, tag,
		 * 0, null, new Date(2011, 12, 10))); tag = tag + tag;
		 * taskDescriptionSample = taskDescriptionSample + i; }
		 */
		// end of dummy events

		/*
		 * Label[] taskDescription = new Label[100]; Label[] dueDate = new
		 * Label[100]; DateFormat formatter = null; for (int i = 0; i < 10; i++)
		 * { taskDescription[i] = new Label(composite_3, SWT.NONE);
		 * taskDescription[i].setBounds(10, 30 + i * 50, 400, 15);
		 * toolkit.adapt(taskDescription[i], true, true);
		 * taskDescription[i].setText(listOfEvents.get(i).DETAILS); /*
		 * dueDate[i] = new Label(composite_3, SWT.NONE);
		 * dueDate[i].setBounds(402, 20+i*50, 400, 15);
		 * toolkit.adapt(dueDate[i], true, true); dueDate[i].setText(formatter
		 * .format(listOfEvents.get(i).ENDDATE));
		 */
		// scrolledComposite_1.setContent(text[i]);
		// scrolledComposite_1.setMinSize(text[i].computeSize(SWT.DEFAULT,
		// SWT.DEFAULT));
		/*
		 * }
		 * 
		 * scrolledComposite_1.setContent(composite_3);
		 * scrolledComposite_1.setMinSize(composite_3.computeSize(SWT.DEFAULT,
		 * SWT.DEFAULT));
		 */
		Label label = new Label(composite_3, SWT.NONE);
		label.setText("Task ");
		label.setBounds(10, 10, 55, 15);
		toolkit.adapt(label, true, true);

		Label label_1 = new Label(composite_3, SWT.NONE);
		label_1.setText("Due Date");
		label_1.setBounds(402, 10, 55, 15);
		toolkit.adapt(label_1, true, true);
		scrolledComposite_1.setContent(composite_3);
		scrolledComposite_1.setMinSize(composite_3.computeSize(SWT.DEFAULT,
				SWT.DEFAULT));
		scrolledComposite_1.setMinSize(new Point(457, 25));

		Button btnMarkAsComplete = new Button(composite_2, SWT.NONE);
		FormData fd_btnMarkAsComplete = new FormData();
		fd_btnMarkAsComplete.right = new FormAttachment(0, 130);
		fd_btnMarkAsComplete.top = new FormAttachment(scrolledComposite_1, 6);
		fd_btnMarkAsComplete.left = new FormAttachment(0, 10);
		btnMarkAsComplete.setLayoutData(fd_btnMarkAsComplete);
		toolkit.adapt(btnMarkAsComplete, true, true);
		btnMarkAsComplete.setText("Mark As Complete");

		Composite composite_4 = new Composite(this, SWT.NONE);
		fd_composite.top = new FormAttachment(composite_4, 2);
		createDragCapabilities(composite_4);
		FormData fd_composite_4 = new FormData();
		fd_composite_4.right = new FormAttachment(100, -50);
		fd_composite_4.left = new FormAttachment(0, 5);
		fd_composite_4.top = new FormAttachment(0);
		fd_composite_4.bottom = new FormAttachment(0, 29);
		composite_4.setLayoutData(fd_composite_4);
		toolkit.adapt(composite_4);
		toolkit.paintBordersFor(composite_4);

		Button btnX = new Button(composite_4, SWT.NONE);
		btnX.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// cleaning up the resources used by the globalhotkey dll
				JIntellitype.getInstance().cleanUp();
				System.exit(1);
			}
		});
		btnX.setBounds(633, 0, 75, 25);
		toolkit.adapt(btnX, true, true);
		btnX.setText("Close");

		Button btnMinimize = new Button(composite_4, SWT.NONE);
		btnMinimize.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				getShell().setVisible(false);
			}
		});
		btnMinimize.setBounds(552, 0, 75, 25);
		toolkit.adapt(btnMinimize, true, true);
		btnMinimize.setText("Minimize");

		// TODO showing menubar
		// showMenuBar();

	}

	private void createDragCapabilities(final Composite composite) {
		composite.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent e) {
				returnMouseUp = false;
				locationOfBeforeClicking = getDisplay().getCursorLocation();
			}

			@Override
			public void mouseUp(MouseEvent e) {
				returnMouseUp = true;
			}
		});

		composite.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent arg0) {
				if (returnMouseUp == false) {
					Point locationOfNewWindow = getDisplay()
							.getCursorLocation();

					Point shellLocation = getShell().getLocation();

					// System.out.println("location of Shell x " +
					// shellLocation.x + " y = "+shellLocation.y);
					// System.out.println("location of New Window x " +
					// locationOfNewWindow.x + " y = "+locationOfNewWindow.y);
					// System.out.println("Location of old window x " +
					// locationOfBeforeClicking.x+
					// "y = "+locationOfBeforeClicking.y );

					locationOfNewWindow = new Point(
							shellLocation.x
									+ (locationOfNewWindow.x - locationOfBeforeClicking.x),
							shellLocation.y
									+ (locationOfNewWindow.y - locationOfBeforeClicking.y));
					getShell().setLocation(locationOfNewWindow);
					locationOfBeforeClicking = getDisplay().getCursorLocation();
				}
			}
		});
	}

	public static void main(String[] args) {

		checkIfGlobalHotKeySupported();

		Display display = new Display();
		final Shell shell = new Shell(display, SWT.NONE);
		userInterface calc = new userInterface(shell, SWT.NONE);
		shell.setSize(745, 100);
		// setting up trayicon
		setTrayIcon(display, shell);

		// starting hotkeys configuration
		registerGlobalHotKey();
		calc.initJIntellitype();

		calc.pack();
		// shell.pack();
		shell.open();
		shell.setText("To Do Manager");

		// setShellBottomRight(shell);
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	// TODO globalHotkeys
	// //////////////////////////////////////////////////////////
	// ////////////////////////////////////////////////////////
	// // ////////////////////global hotkeys code//
	// ////////////////////////////////////////////////////////
	// //////////////////////////////////////////////////////////
	// ////////////////////////////////////////////////////////

	private static void checkIfGlobalHotKeySupported() {
		// first check to see if an instance of this application is already
		// running, use the name of the window title of this JFrame for checking
		if (JIntellitype
				.checkInstanceAlreadyRunning("JIntellitype Test Application")) {
			System.out
					.println("An instance of this application is already running");
			System.exit(1);
		}

		// next check to make sure JIntellitype DLL can be found and we are on
		// a Windows operating System
		if (!JIntellitype.isJIntellitypeSupported()) {
			System.out
					.println("JIntellitype.DLL is not found in the path or this is not Windows 32bit OS.");
			System.exit(1);
		}
	}

	public void initJIntellitype() {
		try {

			// initialize JIntellitype with the frame so all windows commands
			// can
			// be attached to this window
			JIntellitype.getInstance().addHotKeyListener(this);
			JIntellitype.getInstance().addIntellitypeListener(this);
			output("JIntellitype initialized");
		} catch (RuntimeException ex) {
			output("Either you are not on Windows, or there is a problem with the JIntellitype library!");
		}
	}

	private static void registerGlobalHotKey() {

		char quickAddHotKey = 0;
		char quickShowHotKey = 0;

		File inFile = null;
		try {
			inFile = new File("configurationHotKeys.txt");
			inFile.createNewFile();
		} catch (IOException e) {
			System.out.println(e);
		}

		try {

			LineNumberReader fileReader = new LineNumberReader(new FileReader(
					inFile));
			for (int i = 0; i < 2; i++) {
				StringTokenizer seperateDelimeter = new StringTokenizer(
						fileReader.readLine());
				seperateDelimeter.nextToken();
				String hotkey = seperateDelimeter.nextToken();
				if (i == 0) {
					quickAddHotKey = hotkey.charAt(0);
				} else if (i == 1) {
					quickShowHotKey = hotkey.charAt(0);
				}
			}

		} catch (IOException e1) {
		}

		// assign the WINDOWS+A key to the unique id 88 for identification
		// JIntellitype.getInstance().registerHotKey(WINDOWS_A,JIntellitype.MOD_WIN,
		// (int) 'A');
		// JIntellitype.getInstance().registerHotKey(ALT_SHIFT_B,JIntellitype.MOD_ALT
		// + JIntellitype.MOD_SHIFT, (int) 'B');
		JIntellitype.getInstance().registerHotKey(CTRL_SHIFT_A,
				JIntellitype.MOD_CONTROL + JIntellitype.MOD_SHIFT,
				(int) quickAddHotKey);
		JIntellitype.getInstance().registerHotKey(CTRL_SHIFT_S,
				JIntellitype.MOD_CONTROL + JIntellitype.MOD_SHIFT,
				(int) quickShowHotKey);
		// JIntellitype.getInstance().registerSwingHotKey(CTRL_SHIFT_C,
		// Event.CTRL_MASK + Event.SHIFT_MASK, (int) 'C');

		// use a 0 for the modifier if you just want a single keystroke to be a
		// hotkey
		// JIntellitype.getInstance().registerHotKey(F9, 0, 120);
	}

	@Override
	public void onIntellitype(int arg0) {

	}

	@Override
	public void onHotKey(int aIdentifier) {
		if (aIdentifier == 90) {
			String copiedContent = "";
			Clipboard clipboard = Toolkit.getDefaultToolkit()
					.getSystemClipboard();
			// odd: the Object param of getContents is not currently used
			Transferable contents = clipboard.getContents(null);
			boolean hasTransferableText = (contents != null)
					&& contents.isDataFlavorSupported(DataFlavor.stringFlavor);
			if (hasTransferableText) {
				try {
					copiedContent = (String) contents
							.getTransferData(DataFlavor.stringFlavor);
				} catch (UnsupportedFlavorException ex) {
					// highly unlikely since we are using a standard DataFlavor
					System.out.println(ex);
					ex.printStackTrace();
				} catch (IOException ex) {
					System.out.println(ex);
					ex.printStackTrace();
				}
			}

			final String textToPasteToDialog = copiedContent;

			getDisplay().syncExec(new Runnable() {
				public void run() {
					getShell().open();
					getShell().forceActive();
					getShell().setVisible(true);
					commandLineInterface.setText(textToPasteToDialog);
				}
			});

		} else if (aIdentifier == 91) {
			getDisplay().syncExec(new Runnable() {
				public void run() {
					getShell().open();
					getShell().forceActive();
					getShell().setVisible(true);
				}
			});
		}

	}

	private static void output(String text) {
		System.out.println(text);
	}

	// ////////////////////////////////////////////////////////
	// ////////////////////////////////////////////////////////////
	// ////////////////////////////////////////////////////////
	// TODO MENUBAR
	// ////////////////////////////////////////////////////////
	// ********Menubar configuration *************************************
	// ////////////////////////////////////////////////////////
	/*
	 * private void showMenuBar() { Menu menuBar = new Menu(getShell(),
	 * SWT.BAR);
	 * 
	 * MenuItem itemPersonalise = new MenuItem(menuBar, SWT.CASCADE);
	 * itemPersonalise.setText("Personalise");
	 * 
	 * Menu personaliseMenu = new Menu(itemPersonalise);
	 * 
	 * MenuItem personaliseByGroup = new MenuItem(personaliseMenu, SWT.PUSH);
	 * personaliseByGroup.setText("Personalise Hotkey CTRL + G");
	 * personaliseByGroup.setAccelerator(SWT.CTRL + 'G');
	 * personaliseByGroup.addListener(SWT.Selection, new Listener() {
	 * 
	 * @Override public void handleEvent(org.eclipse.swt.widgets.Event arg0) {
	 * Shell shell = new Shell(getDisplay()); personaliseWindow calc = new
	 * personaliseWindow(shell, SWT.NONE); calc.pack(); shell.pack();
	 * shell.open(); shell.setText("Personalise Hotkeys"); } });
	 * 
	 * itemPersonalise.setMenu(personaliseMenu);
	 * 
	 * MenuItem itemCascade = new MenuItem(menuBar, SWT.CASCADE);
	 * itemCascade.setText("Help");
	 * 
	 * Menu menu = new Menu(itemCascade);
	 * 
	 * MenuItem itemPush = new MenuItem(menu, SWT.PUSH);
	 * itemPush.setText("Manual\tCtrl+M"); itemPush.setAccelerator(SWT.CTRL +
	 * 'M'); // Image icon = new Image(shell.getDisplay(), "icons/new.gif"); //
	 * itemPush.setImage(icon); itemPush.addListener(SWT.Selection, new
	 * Listener() {
	 * 
	 * @Override public void handleEvent(org.eclipse.swt.widgets.Event arg0) {
	 * System.out.println("item selected: Manual"); } });
	 * 
	 * new MenuItem(menu, SWT.SEPARATOR); final MenuItem itemAbout = new
	 * MenuItem(menu, SWT.PUSH); itemAbout.setText("About To-do Manager");
	 * itemAbout.setAccelerator(SWT.CTRL + 'R');
	 * itemAbout.addListener(SWT.Selection, new Listener() {
	 * 
	 * @Override public void handleEvent(org.eclipse.swt.widgets.Event arg0) {
	 * System.out.println("item selected: About To-Do Manager"); } });
	 * 
	 * itemCascade.setMenu(menu); getShell().setMenuBar(menuBar); }
	 */
	// *******************************************************************
	// TODO SYSTEMTRAY
	// ******Methods to setSystemTray *************************************//

	private static void setTrayIcon(Display display, final Shell shell) {

		final Tray tray = display.getSystemTray();
		if (tray == null) {
			System.out.println("The system tray is not available");
		} else {
			final TrayItem item = new TrayItem(tray, SWT.NONE);
			item.setToolTipText("ToDo Manager");
			Image trayImage = new Image(display, "./images/systemTray.jpg");
			item.setImage(trayImage);

			createMenuInSystemTray(shell, item);

			item.addListener(SWT.Selection, new Listener() {
				public void handleEvent(org.eclipse.swt.widgets.Event arg0) {
					if (shell.getVisible() == false) {
						shell.setVisible(true);
						shell.forceActive();
					} else
						shell.setVisible(false);

				}
			});

		}

	}

	private static void createMenuInSystemTray(Shell shell, final TrayItem item) {
		final Menu menu = new Menu(shell, SWT.POP_UP);

		createMenuInSystemTrayShow(menu, shell);
		createMenuInSystemTrayExit(menu);
		createMenuInSystemTrayMinimized(menu, shell);

		item.addListener(SWT.MenuDetect, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event arg0) {
				menu.setVisible(true);
			}
		});

	}

	private static void createMenuInSystemTrayShow(final Menu menu,
			final Shell shell) {
		MenuItem menuShow = new MenuItem(menu, SWT.PUSH);
		menuShow.setText("Show");
		menuShow.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}

			public void widgetSelected(SelectionEvent arg0) {
				shell.forceActive();
				shell.setVisible(true);
			}
		});
	}

	private static void createMenuInSystemTrayExit(final Menu menu) {
		MenuItem menuExit = new MenuItem(menu, SWT.PUSH);
		menuExit.setText("Exit");
		menuExit.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}

			public void widgetSelected(SelectionEvent arg0) {
				System.exit(0);
			}
		});
	}

	private static void createMenuInSystemTrayMinimized(final Menu menu,
			final Shell shell) {
		MenuItem menuMinimized = new MenuItem(menu, SWT.PUSH);
		menuMinimized.setText("minimized");
		menuMinimized.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}

			public void widgetSelected(SelectionEvent arg0) {
				shell.setVisible(false);
			}
		});
	}
}
